package cn.yiyuan.car.controller;

import cn.yiyuan.base.until.AjaxResult;
import cn.yiyuan.base.until.PageInfo;
import cn.yiyuan.car.entity.CarTypeEntity;
import cn.yiyuan.car.query.CarTypeQuery;
import cn.yiyuan.car.service.ICarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/carType")
public class CarTypeController {
    @Autowired
    public ICarTypeService carTypeService;


    /**
     * 保存和修改公用的
     * @param carType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody CarTypeEntity carType){
        if( carType.getId()!=null)
            carTypeService.update(carType);
        else
            carTypeService.insert(carType);
        return AjaxResult.success();
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        carTypeService.delete(id);
        return AjaxResult.success();
    }
	
    //获取用户
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id")Long id) {
        CarTypeEntity carType = carTypeService.getById(id);
        return AjaxResult.success(carType);
    }


    /**
    * 查看所有的信息
    * @return
    */
    @GetMapping("/getTypes")
    public AjaxResult queryAll(){
        List< CarTypeEntity> list = carTypeService.getAll();
        return AjaxResult.success(list);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public AjaxResult queryPage(@RequestBody CarTypeQuery query) {
        PageInfo<CarTypeEntity> pageList = carTypeService.queryPage(query);
        return AjaxResult.success(pageList);
    }


    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids) {
        carTypeService.batch(ids);
        return AjaxResult.success();
    }

    /**
     * 车辆类型树的查询
     * @return
     */
    @GetMapping("/treeData")
    public AjaxResult tree(){
        // 这个地方返回的list装的是 一级类型
        List<CarTypeEntity> trees = carTypeService.tree(0L);
        return AjaxResult.success(trees);
    }
}